package be.jabapage.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.lang.Validate;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import be.jabapage.snooker.container.RegionClubContainer;
import be.jabapage.snooker.service.importation.api.IExcelImportService;

/**
 * 
 * Transform an excel file to a binary file for the upload.
 * @author axesq
 *
 */
public class ExcelParser {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(final String[] args) throws Exception {
		Validate.notNull(args);
		if (args.length != 2) {
			System.err
					.println("usage : \n ExcelParser excelFileName binaryFileName");
			System.exit(0);
		}
		String fileName = args[0];
		String binaryFileName = args[1];
		ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-config-services.xml");
		IExcelImportService importService = context
				.getBean(IExcelImportService.class);

		Workbook workBook = new HSSFWorkbook(new FileInputStream(new File(
				fileName)));
		RegionClubContainer container = importService.importExcel(workBook);
		ObjectOutputStream outputStream = new ObjectOutputStream(
				new FileOutputStream(new File(binaryFileName)));
		outputStream.writeObject(container);

	}
}
